﻿using HCQ2_Common;
using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model.VLine.OutputDto;
using HCQ2_Model.VR.Model;
using HCQ2_Model.VR.Params;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRUserMessageDAL:IT_VRUserMessageDAL
    {
        /// <summary>
        ///  获取消息列表
        /// </summary>
        /// <param name="param">查询参数</param>
        /// <param name="total">返回记录数</param>
        /// <returns></returns>
        public List<UserMessageList> GetUserMessageList(UserMessageQuery param, out int total)
        {
            sb?.Clear();
            //记录数
            string totalPram = "SELECT COUNT(0)AS num from ";
            sb.Append($@"(SELECT ROW_NUMBER() OVER(ORDER BY update_date DESC)AS number,user_name,mess_type,mess_content,mess_status,link_url,push_from,
            CONVERT(varchar(100),update_date,120) AS update_date,CONVERT(varchar(100),create_date,120) AS create_date FROM T_VRUserMessage WHERE 1=1 ");
            if (param.mess_type > 0)
                sb.Append($" AND mess_type={param.mess_type} ");
            if (param.push_from > 0)
                sb.Append($" AND push_from={param.push_from} ");
            if (param.mess_status > -2)
                sb.Append($" AND mess_status={param.mess_status} ");
            sb.Append($@") e ");
            total = Helper.ToInt(SqlHelper.ExecuteScalar(totalPram + sb.ToString()));
            return SqlHelper.ExecuteList<UserMessageList>($"SELECT TOP {param.rows} * FROM " + sb.ToString() + $" WHERE e.number>{(param.page - 1) * param.rows} ORDER BY e.number ASC  ");
        }
        //***************************接口**************************
        /// <summary>
        ///  获取用户消息列表
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<UserMessageModel> GetUserMessage(UserMessPram param)
        {
            sb?.Clear();
            sb.Append($@"SELECT user_id,folder_id,send_id,mess_type,mess_content,link_url,push_from 
            FROM dbo.T_VRUserMessage WHERE user_id={param.user_id} AND push_from={param.push_from} ");
            if (param.mess_status > -2)
                sb.Append($" AND mess_status={param.mess_status}  ");
            if(param.mess_type>0)
                sb.Append($" AND mess_type={param.mess_type}  ");
            if (param.folder_id > 0)
                sb.Append($" AND folder_id={param.folder_id}  ");
            return SqlHelper.ExecuteList<UserMessageModel>(sb.ToString());
        }
    }
}